package servlet;

import dao.AdminuserDao;
import dao.impl.AdminuserDaoImpl;
import service.impl.AdminuserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class DoLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("userName");
        String psd = request.getParameter("psd");
        AdminuserDao adminuserDao = new AdminuserDaoImpl();
        AdminuserServiceImpl adminuserService = new AdminuserServiceImpl();
        adminuserService.setAdminuserDao(adminuserDao);

        boolean res = adminuserService.searchAdmin(name,psd);

        if(res){
            HttpSession session = request.getSession();
            session.setAttribute("userName",name);
//            response.sendRedirect("BookServlet");
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }else{
            // 回到登录
            request.setAttribute("msg","请核对用户名和密码");
            // 转发
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
